<html>
  <head>
	<link href="style.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="GenusApis.js"></script>
	<script type="text/javascript">
	var popup_callback_url = "enter popup callback file location here";
	var oOAuthConsumer = new OAuthConsumer("your desktop api key", "your desktop api secret");
	var oGenusApis = new GenusApis(oOAuthConsumer, exceptionHandler, "1.2.1");
	var oOAuthToken = null;
	
	function authorize() {
		oGenusApis.retrieveRequesttoken(new Array("friends.get", "users.get", "albums.getByUser"), null, gotRequesttoken);
	}
	
	function gotRequesttoken(oOAuthRequestToken) {
		oOAuthToken = oOAuthRequestToken;
		window.open(oGenusApis.getAuthorizeUrl(oOAuthToken, popup_callback_url), "_GA_popupWindow", "width=900,height=700,scrollbars=no,status=no,toolbar=no,location=yes");
	}

	function callbackFromPopup(oauth_token) {
		if (oauth_token === oOAuthToken.getKey()) {
			oGenusApis.retrieveAccesstoken(oOAuthToken, gotAccesstoken);
		} else {
			exceptionHandler(new GeneralException("oauth_token from callback unknown."));
		}
	}
	
	function gotAccesstoken(oOAuthAccessToken) {
		oOAuthToken = oOAuthAccessToken;
		document.getElementById('authorizeButton').style.display = "none";
		document.getElementById('friendsButton').style.display = "block";
		document.getElementById('usersButton').style.display = "block";
		document.getElementById('usersresponsefieldsButton').style.display = "block";
		document.getElementById('mediaalbumsButton').style.display = "block";
	}
	
	function friends() {
		document.getElementById('result').innerHTML = "Loading data..";
		oGenusApis.doMethod("friends.get", {}, callbackResponse, oOAuthToken);
	}
	
	function users() {
		document.getElementById('result').innerHTML = "Loading data..";
		oGenusApis.doMethod("users.get", { userid : oOAuthToken.getUserid() }, callbackResponse, oOAuthToken);
	}

	function usersresponsefields() {
		document.getElementById('result').innerHTML = "Loading data..";
		oGenusApis.doMethod("users.get", { userid : oOAuthToken.getUserid(), ha_responsefields : "profilepicture,whitespaces" }, callbackResponse, oOAuthToken);
	}

	function mediaalbums() {
		document.getElementById('result').innerHTML = "Loading data..";
		oGenusApis.doMethod("albums.getByUser", { userid : oOAuthToken.getUserid() }, callbackResponse, oOAuthToken);
	}

	function callbackResponse(data) {
		document.getElementById('result').innerHTML = dataStructureToHtml("results", data);
	}
	
	function exceptionHandler(e) {
		if (e.getType() == 'HyvesApiException') {
			var domResult = document.getElementById('result');
			domResult.innerHTML = dataStructureToHtml("results", e.getResponse());
		}
		alert("Exception occured! (" + e.getCode() + ") " + e.getMessage());
	}
	
	function dataStructureToHtml(key, data) {
		var string = "\"" + key + "\" : ";
		if (data == null) {
			string += "null<br>";
		} else if (typeof(data) == "object") {
			string += "<br>{<br>";
			for (x in data) {
				string += dataStructureToHtml(x, data[x]);
			}
			string += "}<br>";
		} else if (typeof(data) == "number") {
			string += data + "<br>";
		} else {
			string += "\"" + data + "\"<br>";
		}
		return string;
	}
	</script>
  </head>
<body>
<span class="button" id="authorizeButton" onclick="authorize();">Authorize</span><br>
<span class="button invisible" id="friendsButton" onclick="friends();">friends.get</span><br>
<span class="button invisible" id="usersButton" onclick="users();">users.get</span><br>
<span class="button invisible" id="usersresponsefieldsButton" onclick="usersresponsefields();">users.get with ha_responsefields</span><br>
<span class="button invisible" id="mediaalbumsButton" onclick="mediaalbums();">albums.getByUser</span><br>
<span class="result" id="result"></span>
</body>
</html>
